<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AI-SSO-子系统登录页</title>
    <style type="text/css">

    </style>
    <script type="text/javascript" src="/static/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/static/js/common.js?v=318"></script>
    <script type="text/javascript">
        var ai = {};

        // 封装一下Ajax
        ai.ajax = function (url, data, successFn) {
            // sa.loading("正在努力加载...");
            layui.$.ajax({
                url: url,
                type: "get",
                data: data,
                dataType: 'json',
                headers: {
                    satoken: localStorage.getItem('satoken')
                },
                success: function (res) {
                    console.log('返回数据：', res);
                    successFn(res);
                },
                error: function (xhr, type, errorThrown) {
                    if (xhr.status == 0) {
                        return alert('无法连接到服务器，请检查网络');
                    }
                    return alert("异常：" + JSON.stringify(xhr));
                }
            });
        }

    </script>
</head>
<body>
<div class="login-box">

</div>


<!--<script>window.jQuery || alert('当前页面CDN服务商已宕机，请将所有js包更换为本地依赖')</script>-->
<script type="text/javascript">
    layui.use(['layer', 'form', 'notice', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var notice = layui.notice;

        // 后端接口地址
        var baseUrl = "http://localhost:8099/";
        var ssoUrl = "http://localhost:9999/";
        var backUrl = "http://localhost:8099/view/index"



        var back = getParam('back', '/');
        var ticket = getParam('ticket');
        $(function () {
            if (ticket) {
                console.log('存在ticket直接登录')
                doLoginByTicket(ticket);
            } else {
                goSsoAuthUrl();
            }
        })

        // 重定向至认证中心
        function goSsoAuthUrl() {
            let clientLoginUrl = baseUrl + "view/ssoLogin";
            let tmp = ssoUrl + "sso/auth?redirect=" + clientLoginUrl + "&back=" + backUrl;
            console.log("重定向至sso" + tmp)
            location.href = tmp;
        }

        // 根据ticket值登录
        function doLoginByTicket(ticket) {
            $.get(ssoUrl + 'sso/checkTicket?ticket=' + ticket + '&ssoLogoutCall=http://localhost:8099/login/out', function (res) {
                if (res.code === 0) {
                    console.log('检查ticket票据：' + res.data);
                    ai.ajax('/login/doLoginById',{loginId:res.data},function (d) {
                        if (d.code === 0) {
                            console.log('登录结果：')
                            console.log(d)
                            admin.putTempData("tokenInfo", d.data, true);
                            location.href = backUrl;
                        }
                    })

                } else {
                    goSsoAuthUrl()
                }
            })
        }

        // 从url中查询到指定名称的参数值
        function getParam(name, defaultValue) {
            var query = window.top.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == name) {
                    return pair[1];
                }
            }
            return (defaultValue == undefined ? null : defaultValue);
        }
    })
</script>

</body>
</html>
